package com.laf.controller;


import com.github.pagehelper.Page;
import com.laf.model.*;
import com.laf.service.CateService;
import com.laf.service.NoticeService;
import com.laf.service.QuestionService;
import com.laf.service.ScopeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ServletContextAware;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: He Changjie  on  2018-09-27
 * @description:
 */
@Controller
@RequestMapping("/laf")
public class MainController implements ServletContextAware {

    @Resource
    NoticeService noticeService;

    @Resource
    CateService cateService;

    @Resource
    ScopeService scopeService;

    @Resource
    QuestionService questionService;

    /**Servlet容器对象，通过 ServletContextAware 接口注入*/
    ServletContext servletContext;

    @RequestMapping("/bg")
    public String showBackground(){
        return "bk-login";
    }

    @RequestMapping("/showFind")
    public String showFindOne(){
        return "laf-findOne";
    }

    @RequestMapping("/showInfo{type}")
    public String showInfo(@PathVariable int type){
        switch (type){
            case 1:return "laf-inform1";
            case 2:return "laf-inform2";
            case 3:return "laf-inform3";
            case 4:return "laf-inform4";
            case 5:return "laf-inform5";
            case 6:return "laf-inform6";
            default:return "redirect:/laf/showIndex";
        }
    }

    @RequestMapping("/code")
    public void validateCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 设置响应的类型格式为图片格式
        response.setContentType("image/png"); //MIME type: text/html,css,javascript,xml     application/json
        //禁止图像缓存。
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        HttpSession session = request.getSession();

        ValidateCode vCode = new ValidateCode(70,30,4,10);
        String code = vCode.getCode();

        session.setAttribute("vcode",code);

        vCode.write(response.getOutputStream());
    }


    @RequestMapping("/showLogin")
    public String showLogin(){
        return "laf-login";
    }

    @RequestMapping("/showIndex")
    public String showIndex(){

        List<Notice> lostTenList = noticeService.selectByPage(null,1,9,Notice.LOST);
        List<Notice> foundTenList = noticeService.selectByPage(null,1,10,Notice.FOUND);
        servletContext.setAttribute("lostTenList",lostTenList);
        servletContext.setAttribute("foundTenList",foundTenList);

        List<Scope> scopeList= (List<Scope>) servletContext.getAttribute("scopeList");
        if(scopeList==null){
            scopeList=scopeService.findAll();
            List<Category> cateList=cateService.findAll();
            servletContext.setAttribute("scopeList",scopeList);
            servletContext.setAttribute("cateList",cateList);
        }

        List<Integer> infoList= (List<Integer>) servletContext.getAttribute("infoList");
        if(infoList==null){
            infoList=new ArrayList<>();
        }
        infoList.add(noticeService.lostTotal());
        infoList.add(noticeService.lostThreeDay());
        infoList.add(noticeService.foundTotal());
        infoList.add(noticeService.foundThreeDay());
        infoList.add(noticeService.getBack());
        servletContext.setAttribute("infoList",infoList);

        return "redirect:/index.jsp";
    }

    @RequestMapping("/showRegister")
    public String showRegister(){
        List<Question> queList= (List<Question>) servletContext.getAttribute("queList");
        if(queList==null){
            queList=questionService.findAll();
            servletContext.setAttribute("queList",queList);
        }
        return "laf-register";
    }

    @RequestMapping("/showDetail")
    public String showDetail(){
        return "laf-detail";
    }

    @RequestMapping("/showFound")
    public String showFound(HttpSession session){
        Page<Notice> page=(Page<Notice>)noticeService.selectByPage(null, 1, 15, Notice.FOUND);
        session.setAttribute("Page",page);
        session.removeAttribute("Notice");
        return "laf-found";
    }

    @RequestMapping("/showLost")
    public String showLost(HttpSession session){
        Page<Notice> page=(Page<Notice>)noticeService.selectByPage(null, 1, 15, Notice.LOST);
        session.setAttribute("Page",page);
        session.removeAttribute("Notice");
        return "laf-lost";
    }

    @RequestMapping("/showResult")
    public String showResult(){
        return "laf-result";
    }

    @RequestMapping("/showInfo")
    public String showInfo(){
        return "laf-inform";
    }

    @RequestMapping("/showSearch")
    public String showSearch(){
        return "laf-search";
    }

    @RequestMapping("/showPublish")
    public String showPublish(){
        return "publish";
    }

    @RequestMapping("/showPublishFound")
    public String showPublishFound(HttpServletRequest request){
        String id = (String) request.getAttribute("id");
        if(id != null) {
            Notice notice = noticeService.findById(id);
            request.setAttribute("notice",notice);
            request.setAttribute("update","update");
        }
        return "publish-found";
    }

    @RequestMapping("/showPublishLost")
    public String showPublishLost(HttpServletRequest request){
        String id = (String) request.getAttribute("id");
        if(id != null){
            Notice notice = noticeService.findById(id);
            request.setAttribute("notice",notice);
            request.setAttribute("update","update");
        }
        return "publish-lost";
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }
}
